﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.JShop.UI;
using CNVP.Config;
using CNVP.Framework.Utils;

using System.Text;
using System.Data;

namespace CNVP.JShop.Web.Admin.Template
{
    public partial class TemplateSetSet : AdminPage
    {
        BLL.CNVP_Column bll = new BLL.CNVP_Column();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 102)) return;//判断页面权限
            Page.Title = "模版设置";
            if (!Page.IsPostBack)
            {
                CNVP.JShop.BLL.CNVP_Column bll = new CNVP.JShop.BLL.CNVP_Column();
                string ParentID = Public.FilterSql(Request.Params["ParentID"]);
                string typeID = Public.FilterSql(Request.Params["typeID"]);
                string ColumnName = "网站默认";
                string columnType = "0";
                if ((string.IsNullOrEmpty(typeID)) || typeID == "0" || (!Public.IsNumber(typeID)))
                {
                    typeID = "0";
                }
                if ((string.IsNullOrEmpty(ParentID)) || ParentID == "0" || (!Public.IsNumber(ParentID)))
                {
                    ParentID = "0";
                    LitColumnPath.Text = " > 网站默认";
                }
                else
                {
                    LitColumnPath.Text = bll.GetColumnPath("TemplateSet.aspx", Convert.ToInt32(ParentID));
                    ColumnName = bll.GetColumnInfo(Convert.ToInt32(ParentID)).Rows[0]["ColumnName"].ToString();
                    columnType = bll.GetColumnInfo(Convert.ToInt32(ParentID)).Rows[0]["ColumnType"].ToString();
                }
                TemplateParentSet(columnType, ColumnName, ParentID, typeID);
                TemplateChildSet(ParentID, typeID);
            }
        }

        /// <summary>
        /// 主要模板设置
        /// </summary>
        /// <param name="columnType">栏目类型</param>
        /// <param name="ColumnName">栏目名称</param>
        /// <param name="ParentID">父级栏目ID</param>
        private void TemplateParentSet(string columnType, string ColumnName, string ParentID, string typeID)
        {
            StringBuilder Str = new StringBuilder();
            Str.Append("<tr>");
            if (typeID == "3")
            {
                Str.Append("<td height=\"25\">&nbsp;" + ColumnName + "</td>");
                Str.Append("<td height=\"25\">" + TemplateShowSet(columnType, Convert.ToInt32(ParentID), 0) + "</td>");
                Str.Append("<td height=\"25\">" + TemplateShowSet(columnType, Convert.ToInt32(ParentID), 1) + "</td>");
                Str.Append("<td height=\"25\">" + TemplateShowSet(columnType, Convert.ToInt32(ParentID), 2) + "</td>");
            }
            else if (typeID == "2")
            {
                Str.Append("<td height=\"25\">&nbsp;" + ColumnName + "</td>");
                Str.Append("<td height=\"25\">" + TemplateShowSet(columnType, Convert.ToInt32(ParentID), 4) + "</td>");
                Str.Append("<td height=\"25\">" + TemplateShowSet(columnType, Convert.ToInt32(ParentID), 5) + "</td>");
                Str.Append("<td height=\"25\">" + TemplateShowSet(columnType, Convert.ToInt32(ParentID), 6) + "</td>");
            }
            else if (typeID == "1")
            {
                Str.Append("<td height=\"25\">&nbsp;" + ColumnName + "</td>");
                Str.Append("<td height=\"25\">" + TemplateShowSet(columnType, Convert.ToInt32(ParentID), 8) + "</td>");
                Str.Append("<td height=\"25\">" + TemplateShowSet(columnType, Convert.ToInt32(ParentID), 9) + "</td>");
                Str.Append("<td height=\"25\">" + TemplateShowSet(columnType, Convert.ToInt32(ParentID), 10) + "</td>");
            }
            Str.Append("</tr>");
            LitTemplateParentSet.Text = Str.ToString();
        }
        /// <summary>
        /// 二级模板设置
        /// </summary>
        /// <param name="ParentID">父级栏目ID</param>
        /// <returns>模版选择状态</returns>
        private void TemplateChildSet(string ParentID, string typeID)
        {
            CNVP.JShop.BLL.CNVP_Column bll = new CNVP.JShop.BLL.CNVP_Column();
            DataTable Dt1 = bll.GetChildColumnDt(Convert.ToInt32(ParentID), Convert.ToInt32(typeID));
            if (Dt1 != null && Dt1.Rows.Count > 0)
            {
                StringBuilder Str = new StringBuilder();
                for (int i = 0; i < Dt1.Rows.Count; i++)
                {
                    Str.Append("<tr>");
                    if (typeID == "3")
                    {
                        Str.Append("<td width=\"55%\" height=\"25\">&nbsp;" + Dt1.Rows[i]["ColumnName"].ToString() + "</td>");
                        Str.Append("<td width=\"15%\" height=\"25\">" + TemplateShowSet(Dt1.Rows[i]["ColumnType"].ToString(), Convert.ToInt32(Dt1.Rows[i]["ID"]), 0) + "</td>");
                        Str.Append("<td width=\"15%\" height=\"25\">" + TemplateShowSet(Dt1.Rows[i]["ColumnType"].ToString(), Convert.ToInt32(Dt1.Rows[i]["ID"]), 1) + "</td>");
                        Str.Append("<td width=\"15%\" height=\"25\">" + TemplateShowSet(Dt1.Rows[i]["ColumnType"].ToString(), Convert.ToInt32(Dt1.Rows[i]["ID"]), 2) + "</td>");
                    }
                    else if (typeID == "2")
                    {
                        Str.Append("<td width=\"55%\" height=\"25\">&nbsp;" + Dt1.Rows[i]["ColumnName"].ToString() + "</td>");
                        Str.Append("<td width=\"15%\" height=\"25\">" + TemplateShowSet(Dt1.Rows[i]["ColumnType"].ToString(), Convert.ToInt32(Dt1.Rows[i]["ID"]), 4) + "</td>");
                        Str.Append("<td width=\"15%\" height=\"25\">" + TemplateShowSet(Dt1.Rows[i]["ColumnType"].ToString(), Convert.ToInt32(Dt1.Rows[i]["ID"]), 5) + "</td>");
                        Str.Append("<td width=\"15%\" height=\"25\">" + TemplateShowSet(Dt1.Rows[i]["ColumnType"].ToString(), Convert.ToInt32(Dt1.Rows[i]["ID"]), 6) + "</td>");
                    }
                    else if (typeID == "1")
                    {
                        Str.Append("<td width=\"55%\" height=\"25\">&nbsp;" + Dt1.Rows[i]["ColumnName"].ToString() + "</td>");
                        Str.Append("<td width=\"15%\" height=\"25\">" + TemplateShowSet(Dt1.Rows[i]["ColumnType"].ToString(), Convert.ToInt32(Dt1.Rows[i]["ID"]), 8) + "</td>");
                        Str.Append("<td width=\"15%\" height=\"25\">" + TemplateShowSet(Dt1.Rows[i]["ColumnType"].ToString(), Convert.ToInt32(Dt1.Rows[i]["ID"]), 9) + "</td>");
                        Str.Append("<td width=\"15%\" height=\"25\">" + TemplateShowSet(Dt1.Rows[i]["ColumnType"].ToString(), Convert.ToInt32(Dt1.Rows[i]["ID"]), 10) + "</td>");
                    }
                    Str.Append("</tr>");
                }
                LitTemplateChildSet.Text = Str.ToString();
            }
            else
            {
                Panel1.Visible = false;
            }
        }
        /// <summary>
        /// 模板状态
        /// </summary>
        /// <param name="columnType">栏目类型</param>
        /// <param name="ColumnID">栏目ID</param>
        /// <param name="TemplateType">模版类型</param>
        /// <returns>栏目设置链接</returns>
        private String TemplateShowSet(string columnType, int ColumnID, int TemplateType)
        {
            StringBuilder Str = new StringBuilder();
            CNVP.JShop.BLL.CNVP_Template bll = new CNVP.JShop.BLL.CNVP_Template();
            DataTable Dt = bll.GetTemplateInfo(ColumnID, TemplateType, UIConfig.TemplateUse);
            if (Dt != null && Dt.Rows.Count > 0)
            {
                Str.Append("<a href=\"javascript:void(0)\" onclick=\"TemplateDel(" + Dt.Rows[0]["TemplateID"] + ")\">[-]</a>");
                Str.Append("<a href=\"javascript:void(0)\" onclick=\"OpenWindow(" + ColumnID + "," + TemplateType + ")\">[s]</a>");
            }
            else
            {
                Str.Append("<a href=\"javascript:void(0)\" onclick=\"TemplateSelect(" + ColumnID + "," + TemplateType + ");\">[+]</a>");
            }
            return Str.ToString();
        }
    }
}